package com.bstek.demo.service;

import com.bstek.urule.model.ExposeAction;
import com.bstek.urule.model.library.action.annotation.ActionBean;
import com.bstek.urule.model.library.action.annotation.ActionMethod;
import com.bstek.urule.model.library.action.annotation.ActionMethodParameter;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 自定义内置动作
 */
@Component("myInnerAction")
@ActionBean(name="自定义内置动作")
public class MyInnerAction {

    @ActionMethod(name="去空格")
    @ActionMethodParameter(names={"目标字符串"})
    public String trim(String str){
        if(str==null){
            return str;
        }
        return str.trim();
    }

    @ActionMethod(name="截取字符串")
    @ActionMethodParameter(names={"目标字符串","开始位置","结束位置"})
    public String substring(String str,int start,int end){
        return str.substring(start, end);
    }

    @ActionMethod(name="判断字符串是不是数字")
    @ActionMethodParameter(names={"目标字符串"})
    public  boolean isNumber(String str){
       return NumberUtils.isNumber(str);
    }

    @ActionMethod(name="字符串转换成数字")
    @ActionMethodParameter(names={"目标字符串"})
    public  double toNumber(String str){
        if(NumberUtils.isNumber(str)){
            return Double.parseDouble(str);
        }else{
            return 0d;
        }
    }

    @ActionMethod(name="字符串的正则较验")
    @ActionMethodParameter(names={"字符串","正则表达式"})
    public boolean strMatches(String str,String pattern){
        try {
            System.out.println("字符串"+str+" 正则表达式"+pattern);
            return Pattern.matches(pattern, str);
        }catch (Exception e){
            return false;
        }
    }

    public static void main(String[] args) {

//        Date nowDate =  new Date();

        try {
            Date  nowDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2023-05-01 12:00:00");
            Date  begin = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2023-05-01 00:00:00");
            Date  end = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2023-05-03 23:59:00");
            long endTime = end.getTime();
            long beginTime = begin.getTime();
            long dateTime = nowDate.getTime();
            System.out.println(dateTime>beginTime&&dateTime<endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

     }

    @ActionMethod(name="字符串转换成数组")
    @ActionMethodParameter(names={"字符串"})
    public String [] strToArray(String str){
        return str.split("");
    }

    @ActionMethod(name="字符串转换成集合")
    @ActionMethodParameter(names={"字符串"})
    public List<String> strToList(String str){
        return Arrays.asList(str.split(""));
    }



    @ActionMethod(name="是否在时间区间内")
    @ActionMethodParameter(names={"当前时间","开始时间","结束时间"})
    public Boolean isBetween(Date nowDate,String beginDate,String endDate) throws ParseException {
        Date begin = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(beginDate);
        Date end = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endDate);
        long endTime = end.getTime();
        long beginTime = begin.getTime();
        long dateTime = nowDate.getTime();
        return dateTime>beginTime&&dateTime<endTime;
    }

}
